#!/usr/bin/python

import sys
from gi.repository import GObject
gobject = GObject
import dbus
import dbus.mainloop.glib
from dbus import ByteArray

def ApnProvisioningSucceeded(imsi,path):
	print "apnProvisioningSucceeded",imsi,path
	if canexit:
		mainloop.quit()

def ApnProvisioningPartiallySucceeded(imsi,path):
	print "apnProvisioningPartiallySucceeded",imsi,path
	if canexit:
		mainloop.quit()

def ApnProvisioningFailed(imsi,path):
	print "apnProvisioningFailed",imsi,path
	if canexit:
		mainloop.quit()

if __name__ == '__main__':
	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

	bus = dbus.SystemBus()

	provisioning = dbus.Interface(
			bus.get_object('org.nemomobile.provisioning', '/'),
					'org.nemomobile.provisioning.interface')

	provisioning.connect_to_signal("apnProvisioningSucceeded",
					ApnProvisioningSucceeded)
	provisioning.connect_to_signal("apnProvisioningPartiallySucceeded",
					ApnProvisioningPartiallySucceeded)
	provisioning.connect_to_signal("apnProvisioningFailed",
					ApnProvisioningFailed)

	if (len(sys.argv) > 1):
		s1 = dbus.String(sys.argv[1])
	else:
		s1 = dbus.String("")

	s2 = dbus.String("")
	u1 = dbus.UInt32(0)
	u2 = dbus.UInt32(0)
	i1 = int(0)
	i2 = int(0)
	s3 = dbus.String("application/vnd.wap.connectivity-wbxml")

	if (len(sys.argv) > 2):
		file = sys.argv[2]
	else:
		file = 'prov.wbxml'

	f = open(file, 'r')
	content = f.read()
	bytes = ByteArray(content)

	try:
		provisioning.HandleProvisioningMessage(s1,s2,u1,u2,i1,i2,s3,
							bytes, timeout = 0)
	except dbus.exceptions.DBusException:
		pass

	canexit = True

	mainloop = gobject.MainLoop()
	mainloop.run()
